home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-16 | 7.1 KB | 380 lines | [TEXT/CWIE] |
- // NewsWatcher 2.1 "myTools.c"
- // Copyright (C) 1994-1996 Mizutori Tetsuya
- // 1994/5/24, 1994/7/15, 1995/2/8, 1995/2/21, 1995/7/14, 1996/2/12, 1996/8/27, 1996/11/24
-
- #include "myTools.h"
-
- // Global variables
-
- //Boolean gOptionPressed = false; /* true if Option Key is pressed */
-
-
- /***** Keyboard sensing *****/
- #ifdef COMMENT
-
- Apple Standard keyboard Layout
- Examples:
- Code Legend
- 0x00 A
- 0x01 S
- 0x06 Z
- 0x24 Return
- 0x30 Tab
- 0x31 Space
- 0x33 Delete
- 0x35 Escape
-
- #endif /* COMMENT */
-
-
- static Boolean
- IsPressed(
- unsigned short k ) // k = keyboard scan code (0-127 or modifier keys)
- {
- unsigned char km[16];
-
- switch ( k ) {
- case cmdKey: k = 0x37; break;
- case shiftKey: k = 0x38; break;
- case alphaLock: k = 0x39; break;
- case optionKey: k = 0x3A; break;
- case controlKey: k = 0x3B; break;
- default: break;
- }
-
- GetKeys( (void *) km );
-
- return ( ( ( km[k>>3] >> (k & 7) ) ) & 1 != 0 );
- }
-
-
- /**** Kanji code conversion *****/
- #ifdef COMMENT
-
- Kanji code conversion
- Added by Mizutori Tetsuya, 1994/5/24
- JIS Sequence
- Kanji-in: ESC + '$' + { 'B', '@', '(B' }
- Kanji-out: ESC + '(' + { 'J', 'B', 'H' }
-
- #endif /* COMMENT */
-
- #define IN(x,a,b) (((a) <= (x)) && ((x) <= (b)))
-
-
- /**** Kanji code check routines *****/
-
- Boolean
- IsJISKanji(
- const char * p,
- const char * pEnd )
- {
- const unsigned char c1 = * p;
- const unsigned char c2 = * (p+1);
-
- if ( pEnd < p+2 ) return false;
-
- if ( ! IN(c1,0x21,0x7E) ) return false;
- if ( ! IN(c2,0x21,0x7E) ) return false;
-
- return true;
- }
-
-
- Boolean
- IsMSKanji(
- const char * p,
- const char * pEnd )
- {
- const unsigned char c1 = * p;
- const unsigned char c2 = * (p+1);
-
- if ( pEnd < p+2 ) return false;
-
- if ( ! IN(c1,0x81,0x9E) && ! IN(c1,0xE0,0xEC) ) return false;
- if ( ! IN(c2,0x40,0x7E) && ! IN(c2,0x80,0xFC) ) return false;
-
- return true;
- }
-
-
- Boolean
- IsEUCKanji(
- const char * p,
- const char * pEnd )
- {
- const unsigned char c1 = * p;
- const unsigned char c2 = * (p+1);
-
- if ( pEnd < p+2 ) return false;
-
- if ( ( c1 & 0x80 ) == 0 ) return false;
- if ( ( c2 & 0x80 ) == 0 ) return false;
-
- return true;
- }
-
-
-
- Boolean
- IsKanji(
- const char * p,
- const char * pStart,
- const char * pEnd )
- {
- if ( p < pStart ) return false;
- if ( pEnd < p+2 ) return false;
-
- return IsMSKanji( p, pEnd );
- }
-
-
- /***** Kanji code conversion *****/
-
- void
- EUCKanji2JIS(
- const char * p,
- char * q )
- {
- unsigned char c1 = * p;
- unsigned char c2 = * (p+1);
-
- *q = c1 & (unsigned char) 0x7F;
- *(q+1) = c2 & (unsigned char) 0x7F;
- }
-
-
- void
- JIS2EUCKanji(
- const char * p,
- char * q )
- {
- unsigned char c1 = * p;
- unsigned char c2 = * (p+1);
-
- *q = c1 | (unsigned char) 0x80;
- *(q+1) = c2 | (unsigned char) 0x80;
- }
-
-
- void
- MSKanji2JIS(
- const char * p,
- char * q )
- {
- unsigned short d1 = * (unsigned char *) p;
- unsigned short d2 = * (unsigned char *) (p+1);
-
- unsigned short x1;
- unsigned short x2;
-
- x1 = d1 + d1 - ((d1 <= 0x009F) ? 0x00E1 : 0x0161);
- if ( d2 < 0x009F ) x2 = d2 - ((d2 > 0x007F) ? 0x0020 : 0x001F);
- else { x2 = d2 - 0x007E; x1 += 1; }
-
- *q = x1;
- *(q+1) = x2;
- }
-
-
- void
- JIS2MSKanji(
- const char * p,
- char * q )
- {
- unsigned short d1 = * (unsigned char *) p;
- unsigned short d2 = * (unsigned char *) (p+1);
-
- unsigned short x1;
- unsigned short x2;
-
- x1 = ((d1-1)>>1) + ((d1 <= 0x005E) ? 0x0071 : 0x00B1);
- x2 = d2 + ((d1 & 1) ? ((d2 < 0x0060) ? 0x001F : 0x0020) : 0x007E);
-
- *q = x1;
- *(q+1) = x2;
- }
-
-
- /***** Skip punctuations or braces following to Kanji string *****/
-
- long
- SkipOnBrace (
- const char * p,
- const char * pStart,
- const char * pEnd )
- {
- long skipCount;
- unsigned char cp;
- const unsigned char * q;
- const unsigned char romanBraces[16] = ".,;:?!)]}>-\0";
- const unsigned char kanjiBraces[64] = {
- 0xff,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
- 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0xff,0xff,
- 0x60,0xff,0xff,0x63,0x64,0xff,0x66,0xff,0x68,0xff,0x6a,0xff,0x6c,0xff,0x6e,0xff,
- 0x70,0xff,0x72,0xff,0x74,0xff,0x76,0xff,0x78,0xff,0x7a,0xff,0xff,0xff,0xff,0x00,
- };
- // The items of kanjiBraces[] may be assigned in a random order.
- // You are allowed to eliminate the items whose value is 0xff.
- // kanjiBraces[] array must have its last item as '\0'.
- // So, you can also define: kanjiBraces[] = "ABCD...XYZ[\\]`cdfhjlnprtvxz"
- // kanjiBraces[] includes punctuations and right braces in [0x8140 .. 0x817E]
- // which follow the Kanji strings.
- // 0 1 2 3 4 5 6 7 8 9 A B C D E F
- // 0x8140: Å@ÅAÅBÅCÅDÅEÅFÅGÅHÅIÅJÅKÅLÅMÅNÅO 0x40:-123456789ABCDEF
- // 0x8150: ÅPÅQÅRÅSÅTÅUÅVÅWÅXÅYÅZÅ[Å\Å]Å^Å_ 0x50:0123456789ABCD--
- // 0x8160: Å`ÅaÅbÅcÅdÅeÅfÅgÅhÅiÅjÅkÅlÅmÅnÅo 0x60:0--34-6-8-A-C-E-
- // 0x8170: ÅpÅqÅrÅsÅtÅuÅvÅwÅxÅyÅzÅ{Å|Å}Å~** 0x70:0-2-4-6-8-A----*
- // Sorry, these 2-byte characters are shown properly in Kanji font.
-
- // if ( IsPressed(optionKey) || IsPressed(shiftKey) ) return 0;
-
- if ( p < pStart ) return 0;
- if ( pEnd <= p ) return 0;
-
- if ( IsKanji(p,pStart,pEnd) ) {
- if ( *p != (char) 0x81 ) return 0;
- cp = *(p+1);
- q = kanjiBraces;
- skipCount = 2;
- } else {
- cp = *p;
- q = romanBraces;
- skipCount = 1;
- }
-
- while ( *q != '\0' ) {
- if ( cp == *q ) return skipCount;
- q++;
- }
-
- return 0;
- }
-
-
- /**** Memory routines *****/
- #include "memutil.h"
-
- OSErr
- MyResizeHandle(
- void * handle,
- Size len )
- {
- char hState;
- // Size oldLen;
- OSErr err;
-
- // oldLen = GetHandleSize(handle);
-
- hState = MyHGetState(handle);
- MyHUnlock(handle);
-
- err = MySetHandleSize(handle, len);
-
- MyHSetState(handle, hState);
-
- return err;
- }
-
-
- /***** Resource Handling *****/
- long gFillColumn = 74;
-
- #define kFillColumnID 800
- #define iTextBox 4
-
- #define kMoveToFront (WindowPtr)-1L
- #define kNilFilterProc nil
-
- long
- GetFillColumn( void )
- {
- DialogPtr dialog;
- long data;
-
- // data = ( gFillColumn > 0 ? gFillColumn : 74 );
- data = gFillColumn;
-
- if ( ! IsPressed( optionKey ) ) return data;
-
- dialog = GetNewDialog( kFillColumnID, nil, kMoveToFront );
- if ( dialog != nil ) {
- short item;
- short iType;
- Handle iHndl;
- Rect iRect;
- Str255 text;
- OSErr err;
-
- GetDialogItem( dialog, iTextBox, &iType, &iHndl, &iRect );
-
- NumToString( data, text );
- SetDialogItemText( iHndl, text );
- SelectDialogItemText( dialog, iTextBox, 0, 32767 );
-
- // ParamText( prompt, "\p", "\p", "\p" );
-
- err = SetDialogDefaultItem( dialog, ok );
- err = SetDialogCancelItem( dialog, cancel );
- err = SetDialogTracksCursor( dialog, true );
-
- ShowWindow( dialog );
-
- do {
- ModalDialog( kNilFilterProc, &item );
- } while ( item != ok && item != cancel );
-
- GetDialogItemText( iHndl, text );
-
- DisposeDialog( dialog );
-
- if ( item == ok ) StringToNum( text, &data );
- }
-
- if ( data < 10 ) data = 10;
-
- gFillColumn = data;
-
- return data;
- }
-
-
- #ifdef COMMENT
- long GetFillColumn ( void )
- {
- StringHandle theHndl;
- long data = 0;
-
- theHndl = GetString( kFillColumnID );
-
- if ( theHndl != nil ) {
- StringToNum( *theHndl, &data );
- DisposeHandle( (Handle) theHndl );
- }
-
- if ( data < 10 ) data = 10;
-
- return data;
- }
- #endif /* COMMENT */
-
-
- #define kFillMessageID 801
- Boolean
- KillCustomMessageID( void )
- {
- StringHandle theHndl;
- long data = 0;
-
- theHndl = GetString( kFillMessageID );
-
- if ( theHndl != nil ) {
- StringToNum( *theHndl, &data );
- //ReleaseResource( (Handle) theHndl );
- }
-
- return ( data != 0 );
- }
-
- // end of program
-